package com.gbq.qimo.mapper.impl;

import com.gbq.qimo.mapper.TeacherMapper;
import com.gbq.qimo.pojo.Student;
import com.gbq.qimo.pojo.Teacher;
import com.gbq.qimo.utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * @ClassName TeacherMapperImpl
 * @Description TODO
 * @Author guobenqi
 * @Date 2021/5/29 16:01
 */
public class TeacherMapperImpl implements TeacherMapper {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public Teacher findOneTeacher(String uid, String password) {
        try{
            System.out.println(uid);
            System.out.println(password);
            String sql = "select * from teacher where username = ? and password = ?";
            Teacher teacher = template.queryForObject(sql, new BeanPropertyRowMapper<Teacher>(Teacher.class), uid, password);
            return teacher;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Teacher> findAllteacher() {
        String sql = "select T.*,C.cName from teacher T,class C where T.tid = C.cTid";
        List<Teacher> teacherList = template.query(sql, new BeanPropertyRowMapper<>(Teacher.class));
        return teacherList ;
    }

    @Override
    public int addOneTeacher(Teacher teacher) {
        String sql = "insert into teacher values(null,?,?,null,?,?,?,?)";
        int count = template.update(sql,teacher.gettName(),teacher.getUsername(),teacher.gettEmail(),teacher.gettPhone(),teacher.gettAge(),teacher.gettGraduate());

        Teacher tea = template.queryForObject("select tid from teacher where tName = ?",new BeanPropertyRowMapper<>(Teacher.class),teacher.gettName());
        int tid = tea.gettId();
        System.out.println(tid);
        String classSql = "insert into class values(null,?,?)";
        count = template.update(classSql,teacher.getcName(),tid);

        return count;
    }

    @Override
    public Teacher findOneTeacherByUsername(String username) {
        String sql = "select * from teacher where username = ?";
        Teacher teacher = template.queryForObject(sql, new BeanPropertyRowMapper<Teacher>(Teacher.class), username);
        return teacher;
    }

    @Override
    public int register(String username, String password) {
        String sql = "update teacher set password = ? where username = ?";
         int update = template.update(sql, password, username);
        return update;
    }
}
